#include <stdio.h>
#include "sa.h"

		

int main()
{
    sa_t *sa_clt;
    sa_t *sa_srv;
    sa_addr_t *saa_srv;
    sa_addr_t *saa_clt;
    pid_t pid_srv;
    sa_t *sa_cld;
    sa_addr_t *saa_cld;
    char buf_srv[1024];
    char buf_clt[1024];
    size_t l;

#define MSG_TCP_SRV_WELCOME "Welcome client\n"
#define MSG_TCP_CLT_WELCOME "Welcome server\n"
#define MSG_TCP_SRV_GOODBYE "Goodbye client\n"
#define MSG_TCP_CLT_GOODBYE "Goodbye server\n"

        sa_create(&sa_srv);
        sa_option(sa_srv, SA_OPTION_REUSEADDR, 1);
        sa_option(sa_srv, SA_OPTION_REUSEPORT, 1);
        //sa_timeout(sa_srv, SA_TIMEOUT_ALL, 10, 0);
        sa_timeout(sa_srv, SA_TIMEOUT_ACCEPT, 0, 0);

        /* bind socket to local port */
        sa_addr_create(&saa_srv);
        sa_addr_u2a(saa_srv, "inet://127.0.0.1:12345#tcp");
        sa_bind(sa_srv, saa_srv);
        sa_addr_destroy(saa_srv);

        /* receive client connection */
        sa_listen(sa_srv, 10);
        sa_accept(sa_srv, &saa_cld, &sa_cld);
return 0;

        /* communicate with client */
        sa_writef(sa_cld, "%s", MSG_TCP_SRV_WELCOME);
        sa_readln(sa_cld, buf_srv, sizeof(buf_srv), &l);
        if (strcmp(buf_srv, MSG_TCP_CLT_WELCOME) != 0) {
		printf("errore\n");
		return 0;
	}
            /*ts_test_fail(TS_CTX, "server: got \"%s\", expected \"%s\"",                         buf_srv, MSG_TCP_CLT_WELCOME);*/
        sa_writef(sa_cld, "%s", MSG_TCP_SRV_GOODBYE);
        sa_shutdown(sa_cld, "w");
        sa_readln(sa_cld, buf_srv, sizeof(buf_srv), &l);
        if (strcmp(buf_srv, MSG_TCP_CLT_GOODBYE) != 0) {
		printf("errore\n");
		return 0;
	}
            /*ts_test_fail(TS_CTX, "server: got \"%s\", expected \"%s\"",                         buf_srv, MSG_TCP_CLT_GOODBYE);*/
        sa_shutdown(sa_cld, "r");

        /* destroy client connection */
        sa_destroy(sa_cld);
        sa_addr_destroy(saa_cld);

        /* destroy server socket and die */
        sa_destroy(sa_srv);

	return 0;
        //exit(0);

}
